<?php

namespace App\Kuafu\Customer\Controller\Internal;

use App\Kuafu\Activity\Enum\ActivityEnum;
use App\Kuafu\Activity\Service\ActivityService;
use App\Kuafu\Customer\Enum\CodeEnum;
use App\Kuafu\Customer\Enum\PlatformCodeEnum;
use App\Kuafu\Customer\Exceptions\CustomerException;
use App\Kuafu\Customer\Model\CustomerActionLogModel;
use App\Kuafu\Customer\Model\CustomerPlatformModel;
use App\Kuafu\Customer\Service\CustomerActionLogService;
use App\Kuafu\Customer\Service\CustomerPlatformService;
use App\Kuafu\Customer\Validate\CustomerValidate;
use App\Kuafu\Customer\Enum\CustomerCodeEnum;
use App\Kuafu\Customer\Service\CustomerService;


use App\Kuafu\CustomerCoupon\Jobs\SendCouponPolling;
use App\Kuafu\Order\Service\OrderService;
use App\Kuafu\Wechat\Enum\WechatCodeEnum;

use App\Kuafu\Wechat\Service\WechatService;
use App\Support\AppletEncrypt;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Redis;
use App\Kuafu\Base\Controller\BaseController;

class CustomerActionController extends BaseController
{
    /**
     * @param Request $request
     * @return JsonResponse
     */
    public function addCallingCardAction(Request $request): JsonResponse
    {
        $unionId  = $request->unionId ?? '';
        $shopId   = $request->shopId ?? 0;
        $userInfo = CustomerPlatformModel::query()
                                         ->where('unionid', $unionId)
                                         ->where('userId', '!=', 0)
                                         ->first();

        $data = [
            'userId'   => $userInfo->userId ?? 0,
            'unionId'  => $unionId,
            'type'     => CustomerActionLogModel::ACTION_TYPE_APPLET_LEAVE_INFORMATION,
            'userInfo' => [
                'shopId' => $shopId
            ]
        ];

        (new \App\Kuafu\Customer\Service\CustomerActionLogService)->asynSaveData($data);

        return $this->success();
    }

    /**
     * 通过unionId获取用户信息
     * @param Request $request
     * @return JsonResponse
     */
    public function getCustomerPlatformUsers(Request $request): JsonResponse
    {
        $unionIds   = (string)$request->get('unionIds', '');
        $platformId = PlatformCodeEnum::PLATFORM_WECHAT_SERVICE;// 微信服务号

        $unionIds = explode(',', $unionIds);
        if (!$unionIds) {
            return $this->success();
        }

        return $this->success(CustomerPlatformService::getCustomerPlatformUsers($platformId, $unionIds));
    }
}
